package logic

import (
	"context"
	"errors"
	"house/cache"
	"house/model/mysql"
	"time"

	"house/house"
	"house/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ShopAddLogic) ShopAdd(in *house.ShopAddRequest) (*house.ShopAddResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var s mysql.Shop
	s.Price = float64(in.Price)
	s.Num = in.Num
	s.Image = in.Image
	s.Info = in.Info
	s.Title = in.Title
	s.UserId = int(in.UserId)
	var a mysql.AutherRealName
	if a.ID < 0 {
		logx.Error("未实名的用户不能进行发布")
		return nil, errors.New("未实名的用户不能进行发布")
	}
	if err = s.Create(); err != nil {
		logx.Error("添加失败")
		return nil, errors.New("添加失败")
	}
	cache.Rdb.Set(l.ctx, "house", s.ID, time.Minute*5)

	return &house.ShopAddResponse{
		Id: int64(s.ID),
	}, nil
}
